<template>
  <div class="menuLeft-tree m-t-20">
    <div class="panelWrap pan-tree">
      <div class="tree-title title flex flexac flexjcsb fs14 cfff fwb pl p-r-20">
        <div>{{ title }}</div>
      </div>

      <div class="con-tree">
        <el-tree
          ref="SlotTree"
          lazy
          :show-checkbox="true"
          class="111111"
          :current-node-key="0"
          :data="setTree"
          :props="defaultProps"
          :expand-on-click-node="true"
          :highlight-current="true"
          :node-key="NODE_KEY"
          @check-change="handleCheckChange"
        >
          <div slot-scope="{ node, data }" class="con-tr-node">
            <!-- 编辑状态 -->
            <template v-if="node.isEdit">
              <el-input :ref="'slotTreeInput' + data[NODE_KEY]" v-model="data.name" class="xiugxi21321" autofocus size="mini" @blur.stop="handleInput(node, data)" @keyup.enter.native="handleInput(node, data)" />
            </template>
            <!-- 非编辑状态 -->
            <template v-else>
              <el-table-column type="selection" width="55" />
              <!-- 名称： 新增节点增加class（is-new） -->
              <span v-if="type === 0" :class="[data[NODE_KEY] < NODE_ID_START ? 'is-new' : '', 'con-tr-node--name']">
                {{ node.label === 1 ? '本人' : node.label === 2 ? '本机构' : '本机构及下级机构' }}
              </span>
              <span v-else :class="[data[NODE_KEY] < NODE_ID_START ? 'is-new' : '', 'con-tr-node--name']">
                {{ node.label }}
              </span>
            </template>
          </div>
        </el-tree>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Tree',
  props: {
    title: {
      type: String,
      default: ''
    },
    setTree: {
      type: Array,
      default: () => ([])
    },
    defaultProps: {
      type: Object,
      default: () => ({
        children: 'children',
        label: ''
      })
    },
    currentNode: {
      type: Object,
      default: () => ({})
    },
    name: {
      type: String,
      default: ''
    },
    nODEKEY: {
      type: String,
      default: ''
    },
    type: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      checked: true,
      NODE_ID_START: 0
    }
  },
  methods: {
    handleInput(_node, _data) { // 修改节点
      // 退出编辑状态
      if (_node.isEdit) {
        this.$set(_node, 'isEdit', false)
      }
    },
    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate)
    }
  }
}
</script>

<style lang="scss">
.show-btns {
  opacity: 1;
}
.menuLeft-tree {
  overflow: auto;
}
.pan-tree {
  position: relative;
  .tree-title {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1;
  }
}
/* common end */

.con-tree {
  width: 100%;
  width: 200px;
  height: 224px;
  padding-top: 14px;
  overflow: auto;
  // 顶部按钮
  .con-tr-top {
    width: 100px;
    margin-bottom: 2em;
  }
  // 自定义节点
  .con-tr-node {
    // label
    .con-tr-node--name {
      display: inline-block;
      line-height: 40px;
      min-height: 40px;
      // 新增
      &.is-new {
        // font-weight: bold;
      }
    }
    // button
    .con-tr-node--btns {
      margin-left: 10px;
      opacity: 0;
      transition: opacity 0.1s;
      .el-button {
        transform: scale(0.8); // 缩小按钮
        & + .el-button {
          margin-left: -1px;
        }
      }
    }
  }
  // 高亮显示按钮
  .is-current {
    & > .el-tree-node__content {
      .con-tr-node--btns {
        @extend .show-btns;
      }
    }
  }
  // 悬浮显示按钮
  .el-tree-node__content {
    &:hover {
      .con-tr-node--btns {
        @extend .show-btns;
      }
    }
  }
}
</style>
